package com.xunker.appointment.controller;



import com.alibaba.fastjson.JSONObject;

import com.xunker.appointment.exception.AppointmentException;
import com.xunker.appointment.mapper.HospitalSetMapper;
import com.xunker.appointment.model.hospital.HospitalSet;
import com.xunker.appointment.service.ApiService;
import com.xunker.appointment.service.HospitalSetService;
import com.xunker.appointment.utils.HttpRequestHelper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags="医院管理接口")
@Controller
@CrossOrigin
@RequestMapping
public class ApiController extends BaseController{
    @Autowired
    private ApiService apiService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private HospitalSetMapper hospitalSetMapper;
    @RequestMapping("/hospitalSet/index")
    public String getHospitalSet(ModelMap model){
        HospitalSet hospitalSet=hospitalSetService.getById(2);
        model.addAttribute("hospitalSet",hospitalSet);
        return "hospitalSet/index";
    }
    @RequestMapping("/hospitalSet/save")
    public String createHospitalSet(ModelMap model, HttpServletRequest request,RedirectAttributes redirectAttributes){
        try{
            //获取HospitalSet对象
            final Map<String, String[]> resquetMap = request.getParameterMap();
            final Map<String, Object> objectMap = HttpRequestHelper.switchMap(resquetMap);
            final String jsonString = JSONObject.toJSONString(objectMap);
            final HospitalSet hospitalSet_1 = JSONObject.parseObject(jsonString, HospitalSet.class);
            HospitalSet hospitalSet=hospitalSetMapper.selectById(2);
            if (null!=hospitalSet){
                if(StringUtils.isEmpty(hospitalSet.getHospitalCode())||StringUtils.isEmpty(hospitalSet.getSignKey())){
                    this.failureMessage("先设置医院code和签名key",redirectAttributes);
                    return "redirect:/hospitalSet/index";
                }else {
                    hospitalSet.setHospitalCode(hospitalSet_1.getHospitalCode());
                    hospitalSet.setSignKey(hospitalSet_1.getSignKey());
                    hospitalSet.setApiUrl(hospitalSet_1.getApiUrl());
                    hospitalSetMapper.updateById(hospitalSet);//保存
                }
            }else {
                hospitalSet_1.setId(2L);
                hospitalSetMapper.insert(hospitalSet_1);//保存
            }
            final JSONObject hospital = apiService.getHospital();
            model.addAttribute("hospital",hospital);
        }catch (AppointmentException e){
            this.failureMessage(e.getMessage(),request);
        }catch (Exception e){
            this.failureMessage("数据异常",request);
        }
        return "hospital/index";
    }
    @RequestMapping("/hospital/create")
    public String createHospital(ModelMap model){
        return "hospital/create";
    }
    @RequestMapping(value = "/hospital/save")
    public String saveHospital(String data,HttpServletRequest request){
        try {
            apiService.saveHospital(data);
        } catch (AppointmentException e){
            this.failurePage(e.getMessage(),request);
        } catch (Exception e) {
            this.failurePage("数据异常",request);
        }
        return this.successPage(null, request);
    }
    @RequestMapping("/department/list")
    public String findDepartment(ModelMap model,
                                 @RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "10") int pageSize,
                                 HttpServletRequest request,
                                 RedirectAttributes redirectAttributes){
        try {
            HospitalSet hospitalSet=hospitalSetMapper.selectById(2);
            if (null!=hospitalSet){
                if(StringUtils.isEmpty(hospitalSet.getHospitalCode())||StringUtils.isEmpty(hospitalSet.getSignKey())){
                    this.failureMessage("先设置医院code和签名key",redirectAttributes);
                    return "redirect:/hospitalSet/index";
                }else {
                    model.addAllAttributes(apiService.findDepartment(pageNum,pageSize));
                }
            }
        }catch (AppointmentException e){
            this.failureMessage(e.getMessage(),request);
        }catch (Exception e) {
            this.failureMessage("数据异常",request);
        }
        return "department/index";
    }
    @RequestMapping("/department/create")
    public String create(ModelMap model){
        return "department/create";
    }
    @RequestMapping("/department/save")
    public String save(String data,HttpServletRequest request){
        try {
            apiService.saveDepartment(data);
        }catch (AppointmentException e){
            return this.failurePage(e.getMessage(), request);
        } catch(Exception e) {
            return this.failurePage("数据异常", request);
        }
        return this.successPage(null, request);
    }
    @RequestMapping("/schedule/list")
    public String findSchedule(ModelMap model,
                               @RequestParam(defaultValue = "1") int pageNum,
                               @RequestParam(defaultValue = "10") int pageSize,
                               HttpServletRequest request,
                               RedirectAttributes redirectAttributes){
        try {
            HospitalSet hospitalSet=hospitalSetMapper.selectById(2);
            if (null!=hospitalSet){
                if(StringUtils.isEmpty(hospitalSet.getHospitalCode())||StringUtils.isEmpty(hospitalSet.getSignKey())){
                    this.failureMessage("先设置医院code和签名key",redirectAttributes);
                    return "redirect:/hospitalSet/index";
                }else {
                    model.addAllAttributes(apiService.findSchedule(pageNum,pageSize));
                }
            }
        }catch (AppointmentException e){
            this.failureMessage(e.getMessage(),request);
        }catch (Exception e) {
            this.failureMessage("数据异常",request);
        }
        return "schedule/index";
    }
    @RequestMapping("/schedule/create")
    public String createSchedule(ModelMap model){
        return "schedule/create";
    }
    @RequestMapping("/schedule/save")
    public String saveSchedule(String data,HttpServletRequest request){
        try {
            apiService.saveSchedule(data);
        }catch (AppointmentException e){
            return this.failurePage(e.getMessage(), request);
        }catch (Exception e) {
            this.failurePage("数据异常"+e.getMessage(),request);
        }
        return this.successPage(null, request);
    }
    @RequestMapping("/hospital/createBatch")
    public String saveBatchHospital(HttpServletRequest request) {
        try {
            apiService.saveBatchHospital();
        }catch (AppointmentException e){
            this.failurePage(e.getMessage(),request);
        }catch (Exception e) {
            this.failurePage("数据异常",request);
        }
        return this.successPage(null, request);
    }
    @GetMapping("/department/remove/{departmentCode}")
    public String removeDepartment(ModelMap model, @PathVariable String departmentCode,RedirectAttributes redirectAttributes){
        apiService.removeDepartment(departmentCode);
        this.successMessage(null,redirectAttributes);
        return "redirect:/department/list";
    }
    @GetMapping("/schedule/remove/{id}")
    public String removeSchedule(ModelMap model,@PathVariable Long id,RedirectAttributes redirectAttributes){
        apiService.removeSchedule(id);
        this.successMessage(null,redirectAttributes);
        return "redirect:/schedule/list";
    }
}
